package ch11.d5;

public class TicketSell extends Thread{
    static int ticket=100;
    static Object o=new Object();
    @Override
    public void run() {
        while (true){
            if (ticket>0){
               sell();
            }else {
                break;
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public synchronized void sell(){
        if (ticket>0){
            System.out.println(Thread.currentThread().getName()+"卖出了第"+ticket+"张票");
            ticket--;
        }
    }
}
